using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;
#if v3
using Microsoft.ApplicationBlocks.Data; 
#endif
#if v4
using umbraco.DataLayer;
#endif

namespace umbracoRss.plugins.rss
{
    /// <summary>
    /// Summary description for editRss.
    /// </summary>
    public class editRss : umbraco.BasePages.UmbracoEnsuredPage
    {
        protected System.Web.UI.WebControls.TextBox NameTxt;
        protected umbraco.uicontrols.Pane Pane7;
        protected System.Web.UI.WebControls.TextBox UrlTxt;
        protected System.Web.UI.WebControls.TextBox TtlTxt;
        protected System.Web.UI.WebControls.TextBox CategoryTxt;
        protected System.Web.UI.WebControls.TextBox AuthorTxt;
        protected System.Web.UI.WebControls.TextBox CopyrightTxt;
        protected System.Web.UI.WebControls.RadioButtonList fullPath;
        protected System.Web.UI.WebControls.DropDownList descriptionDdl;
        protected System.Web.UI.WebControls.DropDownList pubdatefieldDdl;
        protected System.Web.UI.WebControls.DropDownList filterDocumentTypeDdl;
        protected System.Web.UI.WebControls.DropDownList filterPropertyTypeDdl;
        protected System.Web.UI.WebControls.TextBox filterValue;
        protected System.Web.UI.WebControls.Literal documentTitle;
        protected System.Web.UI.HtmlControls.HtmlInputHidden startNode;
        protected umbraco.uicontrols.UmbracoPanel Panel1;

        private BusinessLogic.RssInstance instance;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // add save button
            ImageButton save = Panel1.Menu.NewImageButton();
            save.ImageUrl = umbraco.GlobalSettings.Path + "/images/editor/save.gif";
            save.Click += new System.Web.UI.ImageClickEventHandler(saveRss_Click);

            if (!IsPostBack)
            {
                instance = new BusinessLogic.RssInstance(int.Parse(umbraco.helper.Request("id")));
                NameTxt.Text = instance.Title;
                UrlTxt.Text = instance.Url;
                TtlTxt.Text = instance.TTL.ToString();
                CategoryTxt.Text = instance.Category;
                AuthorTxt.Text = instance.Author;
                CopyrightTxt.Text = instance.Copyright;
                filterValue.Text = instance.FilterValue;
                updateSource();

                ListItem fullPathTrue = new ListItem("Whole structure (all children)", "true");
                ListItem fullPathFalse = new ListItem("Sub pages only (just below the source)", "false");
                if (instance.FullPath)
                    fullPathTrue.Selected = true;
                else
                    fullPathFalse.Selected = true;
                fullPath.Items.Add(fullPathTrue);
                fullPath.Items.Add(fullPathFalse);

                filterDocumentTypeDdl.Items.Add(new ListItem(umbraco.ui.Text("choose") + "...", ""));
                var dts =
#if v3
 umbraco.cms.businesslogic.web.DocumentType.GetAll; 
#endif
#if v4
 umbraco.cms.businesslogic.web.DocumentType.GetAllAsList();
#endif
                foreach (umbraco.cms.businesslogic.web.DocumentType dt in dts)
                {
                    ListItem li = new ListItem(dt.Text, dt.Alias);
                    if (dt.Alias == instance.DocumentTypeAlias)
                        li.Selected = true;
                    filterDocumentTypeDdl.Items.Add(li);
                }

                descriptionDdl.Items.Add(new ListItem(umbraco.ui.Text("choose") + "...", ""));
                pubdatefieldDdl.Items.Add(new ListItem(umbraco.ui.Text("choose") + "...", ""));
                filterPropertyTypeDdl.Items.Add(new ListItem(umbraco.ui.Text("choose") + "...", ""));
#if v3
                SqlDataReader dr = SqlHelper.ExecuteReader(umbraco.GlobalSettings.DbDSN, CommandType.Text, "select distinct alias from cmspropertyType order by alias");
                while (dr.Read())
                {
                    ListItem li = new ListItem(dr["alias"].ToString());
                    if (instance.DescriptionSourceAlias == dr["alias"].ToString())
                        li.Selected = true;
                    descriptionDdl.Items.Add(li);
                    ListItem li2 = new ListItem(dr["alias"].ToString());
                    if (instance.PropertyTypeAlias == dr["alias"].ToString())
                        li2.Selected = true;
                    filterPropertyTypeDdl.Items.Add(li2);
                    ListItem li3 = new ListItem(dr["alias"].ToString());
                    if (instance.Pubdatefield == dr["alias"].ToString())
                        li3.Selected = true;
                    pubdatefieldDdl.Items.Add(li3);

                }
                dr.Close(); 
#endif
#if v4
                var sql = DataLayerHelper.CreateSqlHelper(umbraco.GlobalSettings.DbDSN);
                var records = sql.ExecuteReader("select distinct alias from cmspropertyType order by alias");
                try
                {
                    while (records.Read())
                    {
                        string alias = records.GetString("alias");
                        ListItem li = new ListItem(alias);
                        if (instance.DescriptionSourceAlias == alias)
                        {
                            li.Selected = true;
                        }
                        descriptionDdl.Items.Add(li);
                        ListItem li2 = new ListItem(alias);
                        if (instance.PropertyTypeAlias == alias)
                            li2.Selected = true;
                        filterPropertyTypeDdl.Items.Add(li2);
                        ListItem li3 = new ListItem(alias);
                        if (instance.Pubdatefield == alias)
                            li3.Selected = true;
                        pubdatefieldDdl.Items.Add(li3);

                    }
                }
                finally
                {
                    records.Dispose();
                }
#endif
            }
        }

        private void updateSource()
        {
            startNode.Value = instance.DocumentId.ToString();
            if (instance.DocumentId != 0)
            {
                documentTitle.Text = "<b>" + new umbraco.cms.businesslogic.web.Document(instance.DocumentId).Text + "</b><br/>";
            }
        }

        private void saveRss_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {

            // Get instance
            instance = new BusinessLogic.RssInstance(int.Parse(umbraco.helper.Request("id")));
            instance.Category = CategoryTxt.Text;
            instance.Author = AuthorTxt.Text;
            instance.Copyright = CopyrightTxt.Text;
            instance.DescriptionSourceAlias = descriptionDdl.SelectedValue;
            instance.Pubdatefield = pubdatefieldDdl.SelectedValue;
            instance.DocumentId = int.Parse(startNode.Value);
            instance.DocumentTypeAlias = filterDocumentTypeDdl.SelectedValue;
            instance.FilterValue = filterValue.Text;
            instance.FullPath = bool.Parse(fullPath.SelectedValue);
            instance.PropertyTypeAlias = filterPropertyTypeDdl.SelectedValue;
            instance.Title = NameTxt.Text;
            instance.TTL = int.Parse(TtlTxt.Text);
            instance.Url = UrlTxt.Text;
            updateSource();
            umbraco.cms.businesslogic.web.Document doc = new umbraco.cms.businesslogic.web.Document(instance.DocumentId);
            bool created = BusinessLogic.Generator.GenerateFromPath(doc.Id, doc.Path, doc);
            this.speechBubble(speechBubbleIcon.save, "RSS Feed updated", "");
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}
